package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 角色管理(角色管理里面有基本信息组合添加角色组)
 */

//角色控制
@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    /**
     * 新增角色
     * @param role
     * @param permissionIds
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody  Role role, Integer[] permissionIds){
        //调用RoleService方法层的add方法
        try {
            roleService.add(role,permissionIds);
            return new Result(true,MessageConstant.ADD_ROLE_SUCCESS);//新增角色成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_ROLE_FAIL);//新增角色失败
        }
    }



    /**
     * 查询所有角色
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        //调用RoleService方法层的findAll方法
        try {
            List<Role> list = roleService.findAll();
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,list);//查询角色数据成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);//查询角色数据失败
        }
    }



    /**
     * 编辑角色管理(就是添加权限)
     * @param role
     * @param permissionIds
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody  Role role, Integer[] permissionIds){
        //调用RoleService方法层的edit方法
        try {
            roleService.edit(role,permissionIds);
            return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);//编辑成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);//编辑失败
        }
    }



    /**
     * 删除角色
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        //调用RoleService方法层的delete方法
        try {
            roleService.delete(id);
            return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);//删除角色成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ROLE_FAIL);//删除角色失败
        }
    }

    /**
     * 通过角色id查找他所包含的所有权限的ids
     * @param id
     * @return
     */
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id) {
        try {
            List<Integer> list = roleService.findPermissionIdsByRoleId(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);

        }
    }

    //查询所有角色信息
    @RequestMapping("/findAllRole")
    public Result findAllRole(){
        try {
            List<Role> role =  roleService.findAllRole();
            return new Result(true, MessageConstant.GET_ROLE_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ROLE_FAIL);

        }
    }
    //通过uid查询所有的rid
    @RequestMapping("/findRoleIdsByUid")
    public Result findRoleIdsByUid(Integer id){
        try {
            System.out.println(id);
            List<Integer> list = roleService.findRoleIdsByUid(id);

            return new Result(true,MessageConstant.GET_ROLE_SUCCESS,list);
        } catch (Exception e) {
            return new Result(false,MessageConstant.GET_ROLE_FAIL);
        }
    }
}
